package com.resource.project.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.resource.base.exception.BadRequestAlertException;
import com.resource.project.entity.RespBean;
import com.resource.project.entity.TResource;
import com.resource.project.entity.TResourceManage;
import com.resource.project.entity.TResourcePlan;
import com.resource.project.mapper.TResourceManageMapper;
import com.resource.project.service.ITResourceManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resource.project.service.ITResourcePlanService;
import com.resource.project.service.ITResourceService;
import com.resource.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 领料/入库单表 服务实现类
 * </p>
 *
 * @author resource
 * @since 2020-12-08
 */
@Service
public class TResourceManageServiceImpl extends ServiceImpl<TResourceManageMapper, TResourceManage> implements ITResourceManageService {

    @Autowired
    private ITResourceService resourceService;
    @Autowired
    private ITResourcePlanService resourcePlanService;
    @Override
    public IPage<TResourceManage> page(PageParam pageParam, TResourceManage resource) {
        Integer type = resource.getType();
        return page(pageParam.toMybaitsPage(),Wrappers.<TResourceManage>lambdaQuery().eq(type!=null,TResourceManage::getType,type))
                .convert(tResourceManage ->tResourceManage.setResource(resourceService.getById(tResourceManage.getResourceId())));
    }

    @Override
    public RespBean add(TResourceManage resource) {
        List<TResourcePlan> list = resourcePlanService.list(Wrappers.<TResourcePlan>lambdaQuery().eq(TResourcePlan::getResourceId, resource.getResourceId()));
        if (list.isEmpty()){
            if(0==resource.getType()){
                //领料
                throw new BadRequestAlertException("暂无相应领料计划！");
            }else if (1==resource.getType()){
                //入库
                throw new BadRequestAlertException("暂无相应采购计划！");
            }
        }
        resource.setStatus(0);
        return RespBean.ok(save(resource));
    }

    @Override
    public RespBean approve(Long id, boolean b) {
        TResourceManage resourceManage = getById(id);
        TResource resource = resourceService.getById(resourceManage.getResourceId());
        if (b){
            if (resourceManage.getType()==0){
                //领料申请
                if (resourceManage.getAmount()>resource.getAmount()){
                    throw new BadRequestAlertException("申请数量大于库存，请仔细核对！");
                }
                resource.setAmount(resource.getAmount()-resourceManage.getAmount());
            }else if (resourceManage.getType()==1){
                //入库申请
                resource.setAmount(NumberUtil.add(resource.getAmount(),resourceManage.getAmount()).intValue());
            }
            resourceService.updateById(resource);
            resourceManage.setStatus(2);
        }else{
            resourceManage.setStatus(1);
        }
        return RespBean.ok(updateById(resourceManage));
    }
}
